#include "func.h"
#include "image.h"
#include "ai.cpp"

void pvp() {
	int flag = 0;
	while (1) {
		if (game(pl1, board))
			break;
		flag++;
		if (flag >= 225) {
			drawWin(0);
			break;
		}
		if ( game(pl2, board) )
			break;
		flag++;
	}
	
}

void pvc1() {
	ai pl1(1);
	int flag = 1;
	prx = 8, pry = 8;
	board[8][8] = 1;
	while (1) {
		if (flag >= 225) {
			drawWin(0);
			break;
		}
		if ( game(pl2, board) )
			break;
		flag ++;
		pl1.aidraw(board);
		flag++;
		int winner;
		if (winner = checkWin(board), winner) {
			drawWin(winner);
			break;
		}
	}
}

void pvc() {
	ai pl2(2);
	int flag = 0;
	while (1) {
		if (flag >= 225) {
			drawWin(0);
			break;
		}
		if ( game(pl1, board) )
			break;
		flag ++;
		pl2.aidraw(board), flag++;
		int winner;
		if (winner = checkWin(board), winner) {
			drawWin(winner);
			break;
		}
	}
}

// 游戏模式选择
int choose() {
	int x, y;
	int flag;
	mouse_msg msg;
	for (bool idx = 1; idx; delay_fps(80)) {
		cleardevice();
		drawboard();
		xyprintf(800, 780, "1");

		putimage_withalpha(NULL, ic, 800, 50, 0, 0);

		rectangle(790, 160, 975, 205);
		putimage_withalpha(NULL, pl1, 800, 170, 0, 0);
		drawchess(880, 235, bl);

		rectangle(790, 290, 975, 335);
		putimage_withalpha(NULL, pl2, 800, 300, 0, 0);
		drawchess(880, 365, wh);

		putimage_withalpha(NULL, gobang, 800, 400, 0, 0);

		rectangle(810, 700, 975, 745);
		putimage_withalpha(NULL, re, 860, 710, 0, 0);

		while (mousemsg()) {
			msg = getmouse();
			if (msg.x > 790 && msg.x < 975 && msg.y > 160 && msg.y < 205 && msg.is_down() && msg.is_left()) {
				flag = 1;
				idx = 0;
				break;
			}
			if (msg.x > 790 && msg.x < 975 && msg.y > 290 && msg.y < 335 && msg.is_down() && msg.is_left()) {
				flag = 2;
				idx = 0;
				break;
			} else if (msg.x > 810 && msg.x < 975 && msg.y > 700 && msg.y < 745 && msg.is_down() && msg.is_left()) {
				flag = 0;
				idx = 0;
				break;
			}
		}

		mousepos(&x, &y);
		flood(x, y, 790, 975, 160, 205, pl1, 10);
		flood(x, y, 790, 975, 290, 335, pl2, 10);
		flood(x, y, 810, 975, 700, 745, re, 50);

	}
	return flag;
}

// 玩游戏
int game(PIMAGE player, char board[17][17]) {
	int x, y;
	bool flag;
	mouse_msg msg;

	for (bool idx1 = 1; idx1; delay_fps(80)) {
//        cleardevice();
		drawboard();
		xyprintf(800, 780, "最终解释权归钟宏展所有");
		chessInboard(board);

		putimage_withalpha(NULL, ic, 800, 50, 0, 0);

		rectangle(790, 160, 975, 205);
		putimage_withalpha(NULL, pl1, 800, 170, 0, 0);
		drawchess(880, 235, bl);

		rectangle(790, 290, 975, 335);
		putimage_withalpha(NULL, pl2, 800, 300, 0, 0);
		drawchess(880, 365, wh);

		putimage_withalpha(NULL, gobang, 800, 400, 0, 0);
		drawchess(prx * 50, pry * 50, idex);

		if (player == pl1)
			flood(800, 170, 790, 975, 160, 205, pl1, 10);
		else
			flood(800, 300, 790, 975, 290, 335, pl2, 10);

		mousepos(&x, &y);
		rectangle(810, 700, 975, 745);
		putimage_withalpha(NULL, re, 860, 710, 0, 0);
		flood(x, y, 810, 975, 700, 745, re, 50);
		
		// 打印虚拟棋子
		if (x > 25 && x < 775 && y > 25 && y < 775) {
			if (!board[(x + 25) / 50][(y + 25) / 50]) {
				if (player == pl1)
					drawchess((x + 25) / 50 * 50, (y + 25) / 50 * 50, vbl);
				else
					drawchess((x + 25) / 50 * 50, (y + 25) / 50 * 50, vwh);
			}
		}
		
		// 监听鼠标动作
		while (mousemsg()) {
			msg = getmouse();
			if (msg.x > 25 && msg.x < 775 && msg.y > 25 && msg.y < 775 && msg.is_down() && msg.is_left()) {
				if (!board[(x + 25) / 50][(y + 25) / 50]) {
					prx = (x + 25) / 50;
					pry = (y + 25) / 50;
					board[(x + 25) / 50][(y + 25) / 50] = player == pl1 ? 1 : 2;
					idx1 = 0;
					flag = 0;
					break;
				}
			} else if (msg.x > 810 && msg.x < 975 && msg.y > 700 && msg.y < 745 && msg.is_down() && msg.is_left()) {
				flag = 1;
				idx1 = 0;
				break;
			}
		}
	}
	int winner = checkWin(board);
	if (winner) {
		flag = winner;
		drawWin(winner);
	}
	return flag;
}

// 判断是否有人赢了
int checkWin(char board[][17]) {
	for (int i = 1; i <= 15; i++) {
		for (int j = 1; j <= 15 ; j++) {
			if (board[i][j]) {
				int flag = 1;
				if (i < 12 && j > 4) {
					for (int k = 1; k <= 4; k++)
						if (board[i][j] != board[i + k][j - k])
							flag = 0;
				} else
					flag = 0;
				if (flag)
					return board[i][j];

				flag = 1;
				if (j < 12) {
					for (int k = 1; k <= 4; k++)
						if (board[i][j] != board[i][j + k])
							flag = 0;
				} else
					flag = 0;
				if (flag)
					return board[i][j];

				flag = 1;
				if (i < 12) {
					for (int k = 1; k <= 4; k++)
						if (board[i][j] != board[i + k][j])
							flag = 0;
				} else
					flag = 0;
				if (flag)
					return board[i][j];

				flag = 1;
				if (j < 12 && i < 12) {
					for (int k = 1; k <= 4; k++)
						if (board[i][j] != board[i + k][j + k])
							flag = 0;
				} else
					flag = 0;
				if (flag)
					return board[i][j];
			}
		}
	}
	return 0;
}
